package com.dongdongshop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.service.ContentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/indexController")
public class IndexController {

    @DubboReference
    private ContentService contentService;

    @Autowired
    private HttpServletRequest request;

    // 根据 ip 获取城市名称
    @RequestMapping("getCity")
    @ResponseBody
    public AjaxResult getCity(){
        //可关闭的连接
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get=new HttpGet("https://www.mxnzp.com/api/ip/self?app_id=isj9kkmrvtezetwn&app_secret=cGlPTnZwM0VOV3ZXQkRMcVZXYWhKQT09");
        CloseableHttpResponse response=null;
        try {
            //响应
            response = client.execute(get);

            //状态码
            int statusCode = response.getStatusLine().getStatusCode();
            AjaxResult result = null;
            if(statusCode==200){
                //主体
                HttpEntity entity = response.getEntity();
                String s = EntityUtils.toString(entity,"utf-8");
                System.out.println(s);
                result = JSON.parseObject(s,AjaxResult.class);

                //确保被关闭
                EntityUtils.consume(entity);
            }
            System.out.println(statusCode);
            return result;

        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }finally {
            if(client!=null){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 根据城市 编码,获取城市当日天气
    @RequestMapping("getWeather")
    @ResponseBody
    public AjaxResult getWeather(String cityId){
        //可关闭的连接
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get=new HttpGet("https://restapi.amap.com/v3/weather/weatherInfo?city="+cityId+"&key=7a1f6553402f20841fd957eab06ee31a");
        CloseableHttpResponse response=null;
        try {
            //响应
            response = client.execute(get);
            String s = null;
            //状态码
            int statusCode = response.getStatusLine().getStatusCode();
            if(statusCode==200){
                //主体
                HttpEntity entity = response.getEntity();
                s = EntityUtils.toString(entity,"utf-8");
                System.out.println(s);

                //确保被关闭
                EntityUtils.consume(entity);
            }
            System.out.println(statusCode);
            return AjaxResult.OK().setData(s);

        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }finally {
            if(client!=null){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 获取城市未来一周天气变化
    @RequestMapping("weekWeather")
    @ResponseBody
    public AjaxResult weekWeather(){
        //可关闭的连接
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get=new HttpGet("https://v0.yiketianqi.com/api?unescape=1&version=v9&appid=23158485&appsecret=Mg1jvbMl");
        CloseableHttpResponse response=null;
        try {
            //响应
            response = client.execute(get);

            //状态码
            int statusCode = response.getStatusLine().getStatusCode();
            AjaxResult result = null;
            if(statusCode==200){
                //主体
                HttpEntity entity = response.getEntity();
                String s = EntityUtils.toString(entity,"utf-8");
                System.out.println(s);
                result = JSON.parseObject(s,AjaxResult.class);

                //确保被关闭
                EntityUtils.consume(entity);
            }
            System.out.println(statusCode);
            return result;

        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }finally {
            if(client!=null){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //获取轮播图
    @RequestMapping("getBanner")
    @ResponseBody
    public AjaxResult<List<TbContent>> getBanner(){
        try {
            List<TbContent> contentList = contentService.getBannerList();
            return AjaxResult.OK().setData(contentList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //获取每日推荐
    @RequestMapping("getDayPush")
    @ResponseBody
    public AjaxResult<List<TbContent>> getDayPush(){
        try {
            List<TbContent> contentList = contentService.getDayPushList();
            return AjaxResult.OK().setData(contentList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //获取猜你喜欢
    @RequestMapping("getYouLike")
    @ResponseBody
    public AjaxResult<List<TbContent>> getYouLike(){
        try {
            List<TbContent> contentList = contentService.getYouLikeList();
            return AjaxResult.OK().setData(contentList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
